//
//  RobbCoreRoute.swift
//  Pods
//
//  Created by eme on 2017/3/24.
//
//

import UIKit
import UtilCore
import URLNavigator
import RxSwift
import RxCocoa
import SwiftyJSON
import RickonCore
import Alamofire
import ModelProtocol
import NetWorkCore

///首页模块的路由
public struct RobbCoreRoute {
    static let disposeBag = DisposeBag()
    public static func initialize() {
        ///跳转到Scan_Vc
        Navigator.map("/scan") { url, values in
            let scanVc:Scan_Vc = RobbCore.storyboard.instantiateViewController()
            Navigator.push(scanVc)
            return true
        }
        ///跳转到 商品详情
        Navigator.map("/productdetail") { url, values in
            let productDetailVc:ProductDetail_Vc = RobbCore.storyboard.instantiateViewController()
            let pdid = url.queryParameters["pdid"]
            productDetailVc.pdid = pdid
            Navigator.push(productDetailVc)
            return true
        }
        /// 跳转到 品牌详情界面
        Navigator.map("/brandpage") { url, values in
            let branPageVc:BranPage_Vc = RobbCore.storyboard.instantiateViewController()
            branPageVc.brid = url.queryParameters["brid"]
            Navigator.push(branPageVc)
            return true
        }
        /// 跳转到 Tjsp_Vc
        Navigator.map("/tjsp") { url, values in
            let tjspVc:Tjsp_Vc = RobbCore.storyboard.instantiateViewController()
            tjspVc.productId = url.queryParameters["id"]
            tjspVc.title = url.queryParameters["title"]
            Navigator.push(tjspVc)
            return true
        }
        ///跳转到Register_Vc
        Navigator.map("/searchpage") { url, values in
            let searchPageVc:SearchPage_Vc = RobbCore.storyboard.instantiateViewController()
            searchPageVc.keyword = url.queryParameters["keyWord"]
            Navigator.push(searchPageVc)
            return true
        }
        /// 跳转到 SearchProducts_Vc
        Navigator.map("/searchproducts") { url, values in
            let searchProductsVc:SearchProducts_Vc = RobbCore.storyboard.instantiateViewController()
            searchProductsVc.keyWord = url.queryParameters["keyWord"]
            searchProductsVc.ptid = url.queryParameters["ptid"]
            searchProductsVc.brid = url.queryParameters["brid"]
            searchProductsVc.vid = url.queryParameters["vid"]
            searchProductsVc.describe = url.queryParameters["describe"]
            searchProductsVc.showTitle = url.queryParameters["title"]
            searchProductsVc.sinmul = url.queryParameters["sinmul"]
            Navigator.push(searchProductsVc)
            return true
        }
        /// 跳转到 秒杀界面
        Navigator.map("/miaosha") { url, values in
            let miaoShaVc:MiaoSha_Vc = RobbCore.storyboard.instantiateViewController()
            miaoShaVc.id = url.queryParameters["id"]
            miaoShaVc.title = url.queryParameters["title"] ?? "限时抢购"
            Navigator.push(miaoShaVc)
            return true
        }
        /// 获取关键字
        Navigator.map("/defaultKeyWord") { url, values in
            Observable.just()
                .map{ HomePageApi.defaultkeyword }
                .emeRequestApiForRegJson()
                .subscribe(onNext: { (result) in
                    switch result {
                    case .success(let json):
                        let keyWord = json["kwname"].stringValue
                        if placeKeyWork.characters.count > 0{
                            NotificationCenter.default.post(name: NSNotification.Name(rawValue: AppConfig.notification.defaultKeyWordKey), object: keyWord)
                        }
                    case .failure(let error):
                        print(error)
                    }
                })
                .disposed(by: RobbCoreRoute.disposeBag)
            return true
        }
        //快速补货
        Navigator.map("/replenishment") { url,values in
            let replenishmentVc:ReplenishmentVc = RobbCore.storyboard.instantiateViewController()
            Navigator.push(replenishmentVc)
            return true
        }
        Navigator.map("/prehomepage") { url, values in
            let homePageTestVc:HomePageTest_Vc = RobbCore.storyboard.instantiateViewController()
            if let paramUrl  = url.queryParameters["url"]{
                Alamofire.request(paramUrl).responseString { response in
                    switch response.result {
                    case.success(let repos):
                        if let dataFromString = repos.data(using: String.Encoding.utf8, allowLossyConversion: false) {
                            let json = JSON(data: dataFromString)
                            if json["status"].intValue == 0{
                                homePageTestVc.itemList = json["data"].arrayValue.flatMap { IndexModel(json: $0) }
                                _ = Navigator.push(homePageTestVc)
                            }
                        }
                    case .failure( let error):
                        print(error)
                    }
                }
            }else{
                showMsg("参数错误~~")
            }
            return true
        }
       
    }
    
}
